package com.xx.bysj.utils;

import com.xx.bysj.dto.HnRole;
import com.xx.bysj.dto.HnUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class SecurityUtils implements UserDetails {

    private Integer id;
    private String username;
    private String password;
    private Boolean enabled = true;//账户是否激活
    private Boolean accountNonExpired = true;//账户是否过期
    private Boolean accountNonLocked = true; //账户是否被锁定
    private Boolean credentialsNonExpired = true;//密码是否过期
    private List<HnRole> roles = new ArrayList<>();//关系属性 用来存储当前用户所有角色信息

    public SecurityUtils() {
    }

    public SecurityUtils(HnUser user) {
        this.id = user.getUserId();
        this.username = user.getUserName();
        /*this.nickname = user.getNickname();*/
        this.password = user.getUserPassword();
        /*this.type = user.getType();*/
        /*this.orgId = user.getOrgId();*/
        /*this.orgIds = user.getOrgIds();*/
        /*this.authorities = Collections.singleton(new SimpleGrantedAuthority(user.getUserRoleId().toString()));*/
    }

    //返回权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority> authorities = new HashSet<>();
        roles.forEach(role -> {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(role.getRoleName());
            authorities.add(simpleGrantedAuthority);
        });
        return authorities;
    }



    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(Boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(Boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<HnRole> getRoles() {
        return roles;
    }

    public void setRoles(List<HnRole> roles) {
        this.roles = roles;
    }

}
